//
//  ViewController.m
//  11104
//
//  Created by 小白 on 16/2/29.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"
long count=0;

@interface ViewController ()

@property (nonatomic,weak) UIScrollView *scrView;
//@property (nonatomic,weak) UIButton *btn0;
//@property (nonatomic,weak) UIButton *btn1;
//@property (nonatomic,weak) UIButton *btn2;
//@property (nonatomic,weak) UIButton *btn3;
//@property (nonatomic,weak) UIButton *btn4;
//@property (nonatomic,weak) UIButton *btn5;
//@property (nonatomic,weak) UIButton *btn6;
//@property (nonatomic,weak) UIButton *btn7;
//@property (nonatomic,weak) UIButton *btn8;
//@property (nonatomic,weak) UIButton *btn9;




@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 22, 320, 40)];
    scrollView.contentSize = CGSizeMake(640, 40);
    scrollView.bounces = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor grayColor];
    self.scrView = scrollView;
    [self.view addSubview:scrollView];

    for (int i=0; i<10; i++) {
        NSString *str = [NSString stringWithFormat:@"新闻%d",i];
        
    
        [self addButtonWithaction:@selector(click:) title:str frame:CGRectMake(i*64, 5, 64, 30) colorred:1 colorgreen:0.66 colorblue:0 inttag:i*10];
    }
//    self.btn0 = [scrollView viewWithTag:0];
//    self.btn1 = [scrollView viewWithTag:1];
//    self.btn2 = [scrollView viewWithTag:2];
//    self.btn3 = [scrollView viewWithTag:3];
//    self.btn4 = [scrollView viewWithTag:4];
//    self.btn5 = [scrollView viewWithTag:5];
//    self.btn6 = [scrollView viewWithTag:6];
//    self.btn7 = [scrollView viewWithTag:7];
//    self.btn8 = [scrollView viewWithTag:8];
//    self.btn9 = [scrollView viewWithTag:9];
    
    
}
-(UIButton *)addButtonWithaction:(SEL)action title:(NSString *)title frame:(CGRect)frame colorred:(float)red colorgreen:(float)green colorblue:(float)blue inttag:(int)i
{
    UIButton *btn = [[UIButton alloc]initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:red green:green blue:blue alpha:1] forState:UIControlStateSelected];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    btn.tag = i;
    [self.scrView addSubview:btn];
    return btn;
}
-(void)click:(id)sender
{
    //    int index = self.scrView.subviews.count;
    //
    //改变一个空间的状态就是把其状态属性改成bool
    //    self.btn1.selected = YES;
       UIButton *button = sender;
//    for (int i = 0; i < 10; i++) {
//        UIButton *btn = [self.scrView viewWithTag:10*i];
//        btn.selected = NO;
//        
//    }
     button.selected = YES;
    for (int i = 0 ; i<10; i++) {
        if (button.tag/10 != count) {
            UIButton *btn = self.scrView.subviews[count];
            btn.selected = NO;
        }
    }
    count = button.tag/10;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
